import os

from django.http import HttpResponse
from django.shortcuts import render
from django.template import loader

from DjangoProject import settings


# Create your views here.

def bbs_username(request,name,age):
    return HttpResponse("名字: %r \t 年龄: %r" % (name,age))

'''
使用templates渲染页面
两种方法
'''
def bbs_username2(request,name,age):
    # 使用loader.get_template获取模板
    t = loader.get_template("show.html")
    # 将模板对象，转换成字符串，可以将一些数据当作参数进行转换，以字典的形式传参
    html = t.render({"name":name, "age":age})
    # 响应一个httpresponse对象
    return HttpResponse(html)

def bbs_views1(request):
    dic = {
        'name':'世界上没有真正的感同身受',
        'author':'王源',
        'player':'王源'
    }
    return render(request,"music.html",dic)


def bbs_views2(request):
    file_path = os.path.join(settings.BASE_DIR, 'bbs', 'test.txt')
    with open(file_path,'r',encoding='utf-8') as f:
        poem = f.read()
    dic = {
        'poem':poem,
    }
    return render(request,"poem.html",dic)


'''
四个页面跳转
'''
def first_views(request):
    return render(request,"first.html")

def second_views(request):
    return render(request,"second.html")

def third_views(request,num):
    return render(request,'third.html',{'num':num})

def fourth_views(request,x,y):
    z = x+y
    return render(request,'fourth.html',{'z':z})


'''
个人信息页
'''
def person_views(request):
    '''
    定义若干个不同类型的变量以字典的形式传递到模板
    '''
    name =  '鹭鸶湾'
    age = 22
    money = 5000.00
    gender = True
    friend = ['江江','窝窝','月月','花花','虎虎','啾啾']
    children = ('孩子1','孩子2')
    dic = {'a':1,'b':2,'c':3}
    set = {1,2,3,55,90}
    fun = lambda x:x+1
    a = A(100)
    # locals()内置函数，会以字典的形式获取当前作用域所有的变量
    return render(request,"person.html",locals())


# A类中可以维护1个data，重写__str__ __repr__
class A():
    def __init__(self,data):
        self.data = data

    # def __str__(self):
    #     return "数据：%d" % self.data

    def __repr__(self):
        return "repr(data):%r" % self.data


'''
这是一个测试的页面的渲染
'''
def test_views(request):
    return render(request,"test.html")

def static_views(request):
    return render(request,"static.html")